package com.simple.javatraining.testing.testinside.questions;

import com.simple.javatraining.testing.Parser.UsersAnswerParser;
import com.simple.javatraining.testing.examinator.Examinator;
import com.simple.javatraining.testing.io.InputOutput;
import com.simple.javatraining.testing.shuffler.Shuffler;
import com.simple.javatraining.testing.testinside.answers.Answer;
import com.simple.javatraining.testing.testinside.answers.ProposedAnswer;

import java.util.List;
import java.util.Set;


public class QuestionWithSeveralAnswers extends Question{

public QuestionWithSeveralAnswers(String textOfQuestion, List<ProposedAnswer> answers) {
    super(textOfQuestion,answers);
}

public double checkUserAnswerForCorrect(Answer userMultiplyAnswer) {
	final List<Answer> parsedUserAnswers =
                                 UsersAnswerParser.parseMultiplyUsersAnswer(userMultiplyAnswer);
	Set<Answer> processedUserAnswers = Examinator.deleteSameAnswers(parsedUserAnswers);
    double numberOfUserCorrectAnswers = 0d;
    
    for (Answer currentUsersAnswer:processedUserAnswers) {
        numberOfUserCorrectAnswers += checkSingleUserAnswerForCorrect(currentUsersAnswer);
    }
    return numberOfUserCorrectAnswers/this.getNumberOfCorrectAnswers();
}

private double checkSingleUserAnswerForCorrect(Answer userAnswer) {
	int chosenVariantOfAnswer = InputOutput.convertStringToInt(userAnswer.getTextOfAnswer());
    List<ProposedAnswer> variantsOfAnswer = this.getVariantsOfAnswer();
    double correspondenceTruth = 0d;
    if (variantsOfAnswer.get(chosenVariantOfAnswer-1).isCorrect()) {
        correspondenceTruth = 1d;
    }
    return correspondenceTruth;
}

public void proposeVariantsOfAnswer() {
    String[] listOfVariants = this.formListOfVariants();
    for(String currentVariant:listOfVariants) {
    		InputOutput.outputMessageToConsole(currentVariant);
    }
}

public void printHint() {
    InputOutput.outputMessageToConsole("Выберите ответы, указав их номера:");
}

public String[] formListOfVariants() {
	List<ProposedAnswer> variantsOfAnswer = this.getVariantsOfAnswer();
	String[] variantsWithNumber = new String[variantsOfAnswer.size()];
    for (int currentIndex = 0; currentIndex < variantsOfAnswer.size(); currentIndex++) {
    	variantsWithNumber[currentIndex] = (currentIndex + 1) + ". "
      + variantsOfAnswer.get(currentIndex).getTextOfAnswer();
    }
    return variantsWithNumber;
}

}
